package com.example.springdemo.Controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.springdemo.service.IUsersService;
import com.example.springdemo.utils.PassToken;
import com.example.springdemo.utils.ResponseResult;
import com.example.springdemo.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户注册信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/system/users")
public class UsersController
{
    @Autowired
    private IUsersService usersService;

    /**
     * 查询用户注册信息列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Users users)
    {
//        startPage();
//        List<Users> list = usersService.selectUsersList(users);
//        return getDataTable(list);
        return ResponseResult.createBySuccess(200,usersService.selectUsersList(users),"查询成功");
    }

    /**
     * 导出用户注册信息列表
     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Users users)
//    {
//        List<Users> list = usersService.selectUsersList(users);
//        ExcelUtil<Users> util = new ExcelUtil<Users>(Users.class);
//        util.exportExcel(response, list, "用户注册信息数据");
//    }

    /**
     * 获取用户注册信息详细信息
     */
    @PassToken
    @GetMapping(value = "/{userid}")
    public ResponseResult getInfo(@PathVariable("userid") String userid)
    {
        Users users = usersService.selectUsersByUserid(userid);
        return ResponseResult.createBySuccess(200,users,"查询成功");
    }

    /**
     * 新增用户注册信息
     */
    @PassToken
    @PostMapping
    public ResponseResult add(@RequestBody Users users)
    {
        int i = usersService.insertUsers(users);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","添加成功");
        }else {
            return ResponseResult.createByFail(500,"添加失败");
        }
    }

    /**
     * 修改用户注册信息
     */
    @PassToken
    @PutMapping
    public ResponseResult edit(@RequestBody Users users)
    {
        int i = usersService.updateUsers(users);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","添加成功");
        }else {
            return ResponseResult.createByFail(500,"添加失败");
        }
    }

    /**
     * 删除用户注册信息
     */
    @PassToken
	@DeleteMapping("/{userids}")
    public ResponseResult remove(@PathVariable String[] userids)
    {
        int i = usersService.deleteUsersByUserids(userids);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","删除成功");
        }else {
            return ResponseResult.createByFail(500,"删除失败");
        }
    }

//    用过username查数据
    @PassToken
    @GetMapping("/list2")
    public ResponseResult selectUsersByUsername(String username){
        Users users = usersService.selectUsersByUsername(username);

            return ResponseResult.createBySuccess(200,users,"查询成功");

    }
}
